<?php
defined("_VALID_ACCESS") || die('Direct access forbidden');
class SCTV_Reports extends Module {
	public function body() {
		$tb = $this->init_module('Utils/TabbedBrowser');		
		$tb->set_tab(__('Statistics') . ':' .__('Acceptance'), array($this, 'deployment'));				
		$tb->set_tab(__('Statistics') . ':' .__('Marketing'), array($this, 'marketing'));
		
		if(Base_AclCommon::check_permission('Statistics:Status'))
			$tb->set_tab(__('Statistics') . ':' .__('Status'), array($this, 'status'));
		
		if(Base_AclCommon::check_permission('Statistics:Todo'))
			$tb->set_tab(__('Statistics') . ':' .__('Todo'), array($this, 'todo'));
		
		$this->display_module($tb);	
		$tb->tag();			
	}
	public function deployment()
	{
		$tb = $this->init_module('Utils/TabbedBrowser');
			
		if(Base_AclCommon::check_permission('Statistics:Acceptance:All'))	
			$tb->set_tab(__('All'), array($this, 'deployment_all'));
		
		if(Base_AclCommon::check_permission('Statistics:Acceptance:Counselor'))
			$tb->set_tab(__('Counselor'), array($this, 'deployment_counselor'));
		
		if(Base_AclCommon::check_permission('Statistics:Acceptance:Deployment Employee'))		
			$tb->set_tab(__('Deployment Employee'), array($this, 'deployment_employee'));
		
		if(Base_AclCommon::check_permission('Statistics:Acceptance:Department'))
			$tb->set_tab(__('Department'), array($this, 'deployment_department'));
		
		if(Base_AclCommon::check_permission('Statistics:Acceptance:Area'))
			$tb->set_tab(__('Area'), array($this, 'deployment_area'));
				
		$this->display_module($tb);	
		$tb->tag();
	}
	public function deployment_all() {
		$form = $this->init_module('Libs/QuickForm',null,__('Statistics') . ': ' . __('All'));
		$form->addElement('datepicker', 'start_date', __('Start Date'));
		$form->addRule('start_date', 'Required', 'required');
		$form->addElement('datepicker', 'end_date', __('End Date'));
		$form->addRule('end_date', 'Required', 'required');
		$form->addElement('submit','submit_button',__('Show'));
		//$submit = HTML_QuickForm::createElement('submit','submit_button',__('Show'));
		//$form->addGroup(array($submit));
				
		if ($form->validate()) {
			$filters = $form->exportValues();								
		}
		else {
			$today = date('Y-m-d');
			$filters = array('start_date'=>$today, 'end_date'=>$today);
		}
		
		$form->setDefaults($filters);		
		$form->display_as_row();
			
		if(isset($_REQUEST['submited']))
		{
			if(isset($_REQUEST['start_date'])) $this->set_module_variable('start_date', $_REQUEST['start_date']);
			if(isset($_REQUEST['end_date'])) $this->set_module_variable('end_date', $_REQUEST['end_date']);					
		}
						
		if($this->isset_module_variable('start_date'))
		{
			$recs = SCTV_RequirementCommon::get_report_deployment_all(strtotime($this->get_module_variable('start_date')), strtotime($this->get_module_variable('end_date')));
			$this->show_result($recs, 'deployment_all');																					
		}
	}

	public function deployment_counselor() {
		$form = $this->init_module('Libs/QuickForm',null,__('Statistics') . ': ' . __('Business'));
		$form->addElement('datepicker', 'start_date', __('Start Date'));
		$form->addRule('start_date', 'Required', 'required');
		$form->addElement('datepicker', 'end_date', __('End Date'));
		$form->addRule('end_date', 'Required', 'required');	
		$form->addElement('autoselect', 'counselor', __('Counselor'), array(), array(array('CRM_ContactsCommon','autoselect_contact_suggestbox'), array(array(), array('CRM_ContactsCommon', 'contact_format_default'))), array('CRM_ContactsCommon', 'contact_format_default'), array('style'=>'width:300px'));
		$form->addRule('counselor', 'Required', 'required');
		$form->addElement('submit','submit_button',__('Show'));
		
		if ($form->validate()) {
			$filters = $form->exportValues();								
		}
		else {
			$today = date('Y-m-d');
			$filters = array('start_date'=>$today, 'end_date'=>$today);
		}
		
		$form->setDefaults($filters);		
		$form->display_as_row();
			
		if(isset($_REQUEST['submited']))
		{
			if(isset($_REQUEST['start_date'])) $this->set_module_variable('start_date', $_REQUEST['start_date']);
			if(isset($_REQUEST['end_date'])) $this->set_module_variable('end_date', $_REQUEST['end_date']);
			if(isset($_REQUEST['counselor'])) $this->set_module_variable('counselor', $_REQUEST['counselor']);					
		}
						
		if($this->isset_module_variable('start_date') && $this->isset_module_variable('counselor'))
		{
            $counselor = $this->get_module_variable('counselor', 0);
			$recs = SCTV_RequirementCommon::get_report_deployment_counselor(strtotime($this->get_module_variable('start_date')), strtotime($this->get_module_variable('end_date')), $counselor);
			$this->show_result_counselor($recs, 'deployment_counselor', $counselor);
		} 
	}
	public function deployment_employee() {
		$form = $this->init_module('Libs/QuickForm',null,__('Statistics') . ': ' . __('Business'));
		$form->addElement('datepicker', 'start_date', __('Start Date'));
		$form->addRule('start_date', 'Required', 'required');
		$form->addElement('datepicker', 'end_date', __('End Date'));
		$form->addRule('end_date', 'Required', 'required');
		$form->addElement('autoselect', 'employee', __('Deployment Employee'), array(), array(array('CRM_ContactsCommon','autoselect_contact_suggestbox'), array(array('group'=>'Technical'), array('CRM_ContactsCommon', 'contact_format_default'))), array('CRM_ContactsCommon', 'contact_format_default'), array('style'=>'width:300px'));
		$form->addRule('employee', 'Required', 'required');
		$form->addElement('submit','submit_button',__('Show'));
		
		if ($form->validate()) {
			$filters = $form->exportValues();								
		}
		else {
			$today = date('Y-m-d');
			$filters = array('start_date'=>$today, 'end_date'=>$today);
		}
		
		$form->setDefaults($filters);		
		$form->display_as_row();
			
		if(isset($_REQUEST['submited']))
		{
			if(isset($_REQUEST['start_date'])) $this->set_module_variable('start_date', $_REQUEST['start_date']);
			if(isset($_REQUEST['end_date'])) $this->set_module_variable('end_date', $_REQUEST['end_date']);
			if(isset($_REQUEST['employee'])) $this->set_module_variable('employee', $_REQUEST['employee']);					
		}
						
		if($this->isset_module_variable(start_date) && $this->isset_module_variable('employee'))
		{			
			$recs = SCTV_RequirementCommon::get_report_deployment_employee(strtotime($this->get_module_variable('start_date')), strtotime($this->get_module_variable('end_date')), $this->get_module_variable('employee', 0));
			$this->show_result($recs, 'deployment_employee');
		} 
	}
	public function deployment_department() {
		$form = $this->init_module('Libs/QuickForm',null,__('Statistics') . ': ' . __('Department'));
		$form->addElement('datepicker', 'start_date', __('Start Date'));
		$form->addRule('start_date', 'Required', 'required');
		$form->addElement('datepicker', 'end_date', __('End Date'));
		$form->addRule('end_date', 'Required', 'required');
		$form->addElement('autoselect', 'department', __('Department'), array(), array(array('CRM_ContactsCommon','autoselect_company_suggestbox'), array(array(), array('CRM_ContactsCommon', 'company_format_default'))), array('CRM_ContactsCommon', 'company_format_default'), array('style'=>'width:300px'));
		$form->addRule('department', 'Required', 'required');
		$form->addElement('submit','submit_button',__('Show'));
		
		if ($form->validate()) {
			$filters = $form->exportValues();								
		}
		else {
			$today = date('Y-m-d');
			$filters = array('start_date'=>$today, 'end_date'=>$today);
		}
		
		$form->setDefaults($filters);		
		$form->display_as_row();
			
		if(isset($_REQUEST['submited']))
		{
			if(isset($_REQUEST['start_date'])) $this->set_module_variable('start_date', $_REQUEST['start_date']);
			if(isset($_REQUEST['end_date'])) $this->set_module_variable('end_date', $_REQUEST['end_date']);
			if(isset($_REQUEST['department'])) $this->set_module_variable('department', $_REQUEST['department']);					
		}
						
		if($this->isset_module_variable('start_date') && $this->isset_module_variable('department'))
		{			
			$recs = SCTV_RequirementCommon::get_report_deployment_department(strtotime($this->get_module_variable('start_date')), strtotime($this->get_module_variable('end_date')), $this->get_module_variable('department', 0));
			$this->show_result($recs, 'deployment_deparment');
		} 
	}
	public function deployment_area() {
		$form = $this->init_module('Libs/QuickForm',null,__('Statistics') . ': ' . __('Area'));
		$form->addElement('datepicker', 'start_date', __('Start Date'));
		$form->addRule('start_date', 'Required', 'required');
		$form->addElement('datepicker', 'end_date', __('End Date'));
		$form->addRule('end_date', 'Required', 'required');
		//$form->addElement('autoselect', 'area', __('Area'), array(), array(array('SCTV_Area'), array(array('param'=>'Name'))));
		$options = SCTV_AreaCommon::get_area(true, 0);
		$form->addElement('select','area',__('Area'), $options);
		$form->addRule('area', 'Required', 'required');
		$form->addElement('submit','submit_button',__('Show'));
		
		if ($form->validate()) {
			$filters = $form->exportValues();								
		}
		else {
			$today = date('Y-m-d');
			$filters = array('start_date'=>$today, 'end_date'=>$today);
		}
		
		$form->setDefaults($filters);		
		$form->display_as_row();
			
		if(isset($_REQUEST['submited']))
		{
			if(isset($_REQUEST['start_date'])) $this->set_module_variable('start_date', $_REQUEST['start_date']);
			if(isset($_REQUEST['end_date'])) $this->set_module_variable('end_date', $_REQUEST['end_date']);
			if(isset($_REQUEST['area'])) $this->set_module_variable('area', $_REQUEST['area']);					
		}
						
		if($this->isset_module_variable('start_date') && $this->isset_module_variable('area'))
		{			
			$recs = SCTV_RequirementCommon::get_report_deployment_area(strtotime($this->get_module_variable('start_date')), strtotime($this->get_module_variable('end_date')), $this->get_module_variable('area', 0));
			$this->show_result($recs, 'deployment_area');
		} 
	}
	public function marketing()
	{
		$tb = $this->init_module('Utils/TabbedBrowser');
		
		if(Base_AclCommon::check_permission('Statistics:Marketing:All'))
			$tb->set_tab(__('All'), array($this, 'marketing_all'));
		
		if(Base_AclCommon::check_permission('Statistics:Marketing:Counselor'))
			$tb->set_tab(__('Counselor'), array($this, 'marketing_counselor'));
		
		if(Base_AclCommon::check_permission('Statistics:Marketing:Department'))
			$tb->set_tab(__('Department'), array($this, 'marketing_department'));
		
		$this->display_module($tb);	
		$tb->tag();
	}
	public function marketing_all()
	{
		$form = $this->init_module('Libs/QuickForm',null,__('Statistics') . ': ' . __('All'));
		$form->addElement('datepicker', 'start_date', __('Start Date'));
		$form->addRule('start_date', 'Required', 'required');
		$form->addElement('datepicker', 'end_date', __('End Date'));
		$form->addRule('end_date', 'Required', 'required');
		$form->addElement('submit','submit_button',__('Show'));
				
		if ($form->validate()) {
			$filters = $form->exportValues();								
		}
		else {
			$today = date('Y-m-d');
			$filters = array('start_date'=>$today, 'end_date'=>$today);
		}
		
		$form->setDefaults($filters);		
		$form->display_as_row();
			
		if(isset($_REQUEST['submited']))
		{
			if(isset($_REQUEST['start_date'])) $this->set_module_variable('start_date', $_REQUEST['start_date']);
			if(isset($_REQUEST['end_date'])) $this->set_module_variable('end_date', $_REQUEST['end_date']);					
		}
						
		if($this->isset_module_variable('start_date'))
		{
			$recs = SCTV_RequirementCommon::get_report_marketing_all(strtotime($this->get_module_variable('start_date')), strtotime($this->get_module_variable('end_date')));
			$this->show_result($recs, 'marketing_all');																	
		}
	}
	public function marketing_counselor()
	{
		$form = $this->init_module('Libs/QuickForm',null,__('Statistics') . ': ' . __('Business'));
		$form->addElement('datepicker', 'start_date', __('Start Date'));
		$form->addRule('start_date', 'Required', 'required');
		$form->addElement('datepicker', 'end_date', __('End Date'));
		$form->addRule('end_date', 'Required', 'required');
		$form->addElement('autoselect', 'counselor', __('Counselor'), array(), array(array('CRM_ContactsCommon','autoselect_contact_suggestbox'), array(array(), array('CRM_ContactsCommon', 'contact_format_default'))), array('CRM_ContactsCommon', 'contact_format_default'), array('style'=>'width:300px'));
		$form->addRule('counselor', 'Required', 'required');
		$form->addElement('submit','submit_button',__('Show'));
		
		if ($form->validate()) {
			$filters = $form->exportValues();								
		}
		else {
			$today = date('Y-m-d');
			$filters = array('start_date'=>$today, 'end_date'=>$today);
		}
		
		$form->setDefaults($filters);		
		$form->display_as_row();
			
		if(isset($_REQUEST['submited']))
		{
			if(isset($_REQUEST['start_date'])) $this->set_module_variable('start_date', $_REQUEST['start_date']);
			if(isset($_REQUEST['end_date'])) $this->set_module_variable('end_date', $_REQUEST['end_date']);
			if(isset($_REQUEST['counselor'])) $this->set_module_variable('counselor', $_REQUEST['counselor']);					
		}
						
		if($this->isset_module_variable('start_date') && $this->isset_module_variable('counselor'))
		{					
			$recs = SCTV_RequirementCommon::get_report_marketing_counselor(strtotime($this->get_module_variable('start_date')), strtotime($this->get_module_variable('end_date')), $this->get_module_variable('counselor', 0));
			$this->show_result($recs, 'marketing_counselor');
		}
	}
	public function marketing_department() {
		$form = $this->init_module('Libs/QuickForm',null,__('Statistics') . ': ' . __('Department'));
		$form->addElement('datepicker', 'start_date', __('Start Date'));
		$form->addRule('start_date', 'Required', 'required');
		$form->addElement('datepicker', 'end_date', __('End Date'));
		$form->addRule('end_date', 'Required', 'required');
		$form->addElement('autoselect', 'department', __('Department'), array(), array(array('CRM_ContactsCommon','autoselect_company_suggestbox'), array(array(), array('CRM_ContactsCommon', 'company_format_default'))), array('CRM_ContactsCommon', 'company_format_default'), array('style'=>'width:300px'));
		$form->addRule('department', 'Required', 'required');
		$form->addElement('submit','submit_button',__('Show'));
		
		if ($form->validate()) {
			$filters = $form->exportValues();								
		}
		else {
			$today = date('Y-m-d');
			$filters = array('start_date'=>$today, 'end_date'=>$today);
		}
		
		$form->setDefaults($filters);		
		$form->display_as_row();
			
		if(isset($_REQUEST['submited']))
		{
			if(isset($_REQUEST['start_date'])) $this->set_module_variable('start_date', $_REQUEST['start_date']);
			if(isset($_REQUEST['end_date'])) $this->set_module_variable('end_date', $_REQUEST['end_date']);
			if(isset($_REQUEST['department'])) $this->set_module_variable('department', $_REQUEST['department']);					
		}
						
		if($this->isset_module_variable('start_date') && $this->isset_module_variable('department'))
		{			
			$recs = SCTV_RequirementCommon::get_report_marketing_department(strtotime($this->get_module_variable('start_date')), strtotime($this->get_module_variable('end_date')), $this->get_module_variable('department', 0));
			$this->show_result($recs, 'marketng_deparment');
		} 
	}
	public function status() {
		$form = $this->init_module('Libs/QuickForm',null,__('Statistics') . ': ' . __('All'));
		
		$status = Utils_CommonDataCommon::get_translated_array('SCTV/Requirement/Status');
		unset($status[1]);
		unset($status[2]);
		$status['']='---';
		$form->addElement('select','status',__('Status'), $status, array('onchange'=>$form->get_submit_form_js(true), 'style'=>'width:200px'));
		$form->addRule('status', 'Required', 'required');

		if ($form->validate()) {
			$filters = $form->exportValues();								
		}
		else {
			$filters = array('status'=>'');
		}
		
		$form->setDefaults($filters);		
		$form->display_as_row();
			
		if(isset($_REQUEST['submited']))
		{
			if(isset($_REQUEST['status'])) $this->set_module_variable('status', $_REQUEST['status']);					
		}
						
		if($this->isset_module_variable('status'))
		{
			$recs = SCTV_RequirementCommon::get_report_status($this->get_module_variable('status'));
			$this->show_result($recs, 'status');																					
		}
	}
	public function todo() {
		$form = $this->init_module('Libs/QuickForm',null,__('Statistics') . ': ' . __('All'));
		
		$status = Utils_CommonDataCommon::get_translated_array('SCTV/Requirement/Todo');		
		$status['']='---';
		$form->addElement('select','todo',__('Todo'), $status, array('onchange'=>$form->get_submit_form_js(true), 'style'=>'width:200px'));
		$form->addRule('todo', 'Required', 'required');

		if ($form->validate()) {
			$filters = $form->exportValues();								
		}
		else {
			$filters = array('todo'=>'');
		}
		
		$form->setDefaults($filters);		
		$form->display_as_row();
			
		if(isset($_REQUEST['submited']))
		{
			if(isset($_REQUEST['todo'])) $this->set_module_variable('todo', $_REQUEST['todo']);					
		}
						
		if($this->isset_module_variable('todo'))
		{
			$recs = SCTV_RequirementCommon::get_report_todo($this->get_module_variable('todo'));
			$this->show_result($recs, 'todo');																					
		}
	}
	private function show_result($recs, $report)
	{				
		$gb = $this->init_module('Utils/GenericBrowser',null,'statictis');
		$gb->set_table_columns(array(
				array('name'=>__('Customer Group'), 'width'=>'90px'),
				array('name'=>__('Customer Name'), 'width'=>'120px'),
				array('name'=>__('Address'), 'width'=>'70px'),
				array('name'=>__('Street'), 'width'=>'100px'),
				array('name'=>__('Area'), 'width'=>'70px'),
				array('name'=>__('Ward'), 'width'=>'70px'),
				array('name'=>__('District'), 'width'=>'60px'),				
				array('name'=>__('Phone'), 'width'=>'80px'),
				array('name'=>__('Cable Status'), 'width'=>'80px'),
                array('name'=>__('Main Analog'), 'width'=>'80px'),
                array('name'=>__('Sub Analog'), 'width'=>'80px'),
				array('name'=>__('Number Of HD'), 'width'=>'80px'),
				array('name'=>__('Number Of SD'), 'width'=>'80px'),
				array('name'=>__('Internet Package'), 'width'=>'90px'),
				array('name'=>__('Bundle'), 'width'=>'80px'),
				array('name'=>__('Counselor'), 'width'=>'120px'),				
				array('name'=>__('Input Date'), 'width'=>'70px'),
				array('name'=>__('Input Time'), 'width'=>'70px'),
				array('name'=>__('Delivery Date'), 'width'=>'70px'),				
				array('name'=>__('Requirement Code'), 'width'=>'70px'),
				array('name'=>__('Note'), 'width'=>'100px'),
				array('name'=>__('Deployment Date'), 'width'=>'90px'),
				array('name'=>__('Deployment Time'), 'width'=>'90px'),
				array('name'=>__('Acceptance Date'), 'width'=>'90px'),
				array('name'=>__('Deployment Employee'), 'width'=>'120px'),
				array('name'=>__('Deployment Cable Status'), 'width'=>'80px'),
                array('name'=>__('Deployment Main Analog'), 'width'=>'90px'),
                array('name'=>__('Deployment Sub Analog'), 'width'=>'90px'),
				array('name'=>__('Deployment HD'), 'width'=>'90px'),
				array('name'=>__('Deployment SD'), 'width'=>'90px'),
				array('name'=>__('Deployment Internet Package'), 'width'=>'100px'),
				array('name'=>__('Deployment Bundle'), 'width'=>'90px'),
				array('name'=>__('Deployment Business'), 'width'=>'90px'),
				array('name'=>__('Cable Contract Code'), 'width'=>'100px'),
				array('name'=>__('Internet Contract Code'), 'width'=>'100px'),
				array('name'=>__('Customer Code'), 'width'=>'100px'),
				array('name'=>__('Tap Code'), 'width'=>'100px'),
				array('name'=>__('Lend Wifi'), 'width'=>'50px'),
				array('name'=>__('Charge'), 'width'=>'100px'),
				array('name'=>__('Secondary Counselor'), 'width'=>'120px'),
                array('name'=>__('Secondary Main Analog'), 'width'=>'90px'),
                array('name'=>__('Secondary Sub Analog'), 'width'=>'90px'),
				array('name'=>__('Secondary HD'), 'width'=>'90px'),
				array('name'=>__('Secondary SD'), 'width'=>'90px'),
				array('name'=>__('Secondary Internet Package'), 'width'=>'100px')
			));
		
		$tong_so_phieu_yeu_cau = 0;
		$tong_so_phieu_thc_yeu_cau = 0;
        $tong_so_analog_diem_chinh_yeu_cau = 0;
        $tong_so_analog_diem_phu_yeu_cau = 0;
		$tong_so_hd_yeu_cau = 0;	
		$tong_so_sd_yeu_cau = 0;
		$tong_so_phieu_net_yeu_cau = 0;
		$tong_so_phieu_tron_goi_yeu_cau = 0;
		
		$tong_so_phieu_nghiem_thu = 0;
		$tong_so_phieu_thc_nghiem_thu = 0;
        $tong_so_analog_diem_chinh_nghiem_thu = 0;
        $tong_so_analog_diem_phu_nghiem_thu = 0;
		$tong_so_hd_nghiem_thu = 0;	
		$tong_so_sd_nghiem_thu = 0;
		$tong_so_phieu_net_nghiem_thu = 0;
		$tong_so_phieu_tron_goi_nghiem_thu = 0;
								
		$internetpackages = array();
		$bundles = array();
		
		foreach ($recs as $key => $value){			
			$tong_so_phieu_yeu_cau++;

            $main_analog = $value['main_analog'];
            $sub_analog = $value['sub_analog'];
            $number_of_hd = $value['number_of_hd'];
            $number_of_sd = $value['number_of_sd'];
            if($number_of_hd > 0 || $number_of_sd > 0 || $main_analog > 0 || $sub_analog > 0)
            {
                $tong_so_phieu_thc_yeu_cau++;
                if($main_analog > 0)
                {
                    $tong_so_analog_diem_chinh_yeu_cau += $main_analog;
                }
                if($sub_analog > 0)
                {
                    $tong_so_analog_diem_phu_yeu_cau += $sub_analog;
                }
                if($number_of_hd > 0)
                {
                    $tong_so_hd_yeu_cau += $number_of_hd;
                }
                if($number_of_sd > 0)
                {
                    $tong_so_sd_yeu_cau += $number_of_sd;
                }
            }

			$internet_package = $value['internet_package'];
			if($internet_package != '' && $internet_package != null)
			{
				$tong_so_phieu_net_yeu_cau++;
				
				if(array_key_exists($internet_package, $internetpackages))
					$internetpackages[$internet_package][0] = $internetpackages[$internet_package][0] + 1; 
				else
				{
					$internetpackages[$internet_package] = array(1, 0);					
				}
			}
			
			$bundle = $value['bundle'];
			if($bundle != '' && $bundle != null)
			{
				$tong_so_phieu_tron_goi_yeu_cau++;
				
				if(array_key_exists($bundle, $bundles))
					$bundles[$bundle][0] = $bundles[$bundle][0] + 1; 
				else
				{
					$bundles[$bundle] = array(1, 0);					
				}
			}
			
			$status = $value['status'];
			if($status == '1')
			{
				$tong_so_phieu_nghiem_thu++;

                $deployment_main_analog = $value['deployment_main_analog'];
                $deployment_sub_analog = $value['deployment_sub_analog'];
				$deployment_hd = $value['deployment_hd'];
				$deployment_sd = $value['deployment_sd'];
				if($deployment_hd > 0 || $deployment_sd > 0 || $deployment_main_analog > 0 || $deployment_sub_analog > 0)
				{
					$tong_so_phieu_thc_nghiem_thu++;
                    if($deployment_main_analog > 0) {
                        $tong_so_analog_diem_chinh_nghiem_thu += $deployment_main_analog;
                    }
                    if($deployment_sub_analog > 0) {
                        $tong_so_analog_diem_phu_nghiem_thu += $deployment_sub_analog;
                    }
					if($deployment_hd > 0)
					{
						$tong_so_hd_nghiem_thu += $deployment_hd;
					}
					if($deployment_sd > 0)
					{
						$tong_so_sd_nghiem_thu += $deployment_sd;
					}
				}
				
				$internet_package = $value['deployment_internet_package'];
				if($internet_package != '' && $internet_package != null)
				{
					$tong_so_phieu_net_nghiem_thu++;
					
					if(array_key_exists($internet_package, $internetpackages))
						$internetpackages[$internet_package][1] = $internetpackages[$internet_package][1] + 1; 
					else
					{
						$internetpackages[$internet_package] = array(0, 1);					
					}
				}
			
				$bundle = $value['deployment_bundle'];
				if($bundle != '' && $bundle != null)
				{
					$tong_so_phieu_tron_goi_nghiem_thu++;
					
					if(array_key_exists($bundle, $bundles))
						$bundles[$bundle][1] = $bundles[$bundle][1] + 1; 
					else
					{
						$bundles[$bundle] = array(0, 1);					
					}
				}
			}
			
			$gb->add_row(
					$value['customer_group'], 
					$value['customer_name'],
					$value['address'],
					$value['street'],
					$value['area'],
					$value['ward'],
					$value['district'],					
					$value['phone'],
					SCTV_RequirementCommon::get_cable_status_name($value['cable_status']),
                    $value['main_analog'],
                    $value['sub_analog'],
					$value['number_of_hd'],
					$value['number_of_sd'],
					$value['internet_package'],
					$value['bundle'],
					$value['counselor'],
					date('d-m-Y', strtotime($value['input_date'])),
					$value['input_time'],
					$value['delivery_date'],					
					$value['requirement_code'],
					$value['note'],
					$value['deployment_date'] == null?null:date('d-m-Y', strtotime($value['deployment_date'])),
					$value['deployment_time'],
					$value['acceptance_date'] == null?null:date('d-m-Y', strtotime($value['acceptance_date'])),
					$value['deployment_employee'],
					SCTV_RequirementCommon::get_cable_status_name($value['deployment_cable_status']),
                    $value['deployment_main_analog'],
                    $value['deployment_sub_analog'],
					$value['deployment_hd'],
					$value['deployment_sd'],
					$value['deployment_internet_package'],
					$value['deployment_bundle'],
					$value['deployment_business'],
					$value['cable_contract_code'],
					$value['internet_contract_code'],
					$value['customer_code'],
					$value['tap_code'],
					SCTV_RequirementCommon::get_lend_wifi_name($value['lend_wifi']),
					$value['charge'],
					$value['secondary_counselor'],
                    $value['secondary_main_analog'],
                    $value['secondary_sub_analog'],
					$value['secondary_hd'],
					$value['secondary_sd'],
					$value['secondary_internet_package']
			);
		}

		$this->display_module($gb, array(true),'automatic_display');
		Base_ActionBarCommon::add('save', __('Export CSV'), 'href="modules/SCTV/Reports/'. $report . '_export.php?'.http_build_query(array('report'=>$report, 'cid'=>CID, 'path'=>$this->get_path())).'"');
		Base_ActionBarCommon::add('save', __('Export Excel'), 'href="modules/SCTV/Reports/'. $report . '_excel.php?'.http_build_query(array('report'=>$report, 'cid'=>CID, 'path'=>$this->get_path())).'"');
		
		$gb_summary = $this->init_module('Utils/GenericBrowser',null,'summary');
		$gb_summary->set_table_columns(array(
				array('name'=>__('Content'), 'width'=>'20%'),
				array('name'=>__('Requirement'), 'width'=>'10%'),
				array('name'=>__('Completed'), 'width'=>'10%')
		));
		
		$gb_summary->add_row('Tổng số phiếu', $tong_so_phieu_yeu_cau, $tong_so_phieu_nghiem_thu);
		$gb_summary->add_row('Tổng số phiếu: THC', $tong_so_phieu_thc_yeu_cau, $tong_so_phieu_thc_nghiem_thu);
        $gb_summary->add_row('- Số Analog điểm chính', $tong_so_analog_diem_chinh_yeu_cau, $tong_so_analog_diem_chinh_nghiem_thu);
        $gb_summary->add_row('- Số Analog điểm phụ', $tong_so_analog_diem_phu_yeu_cau, $tong_so_analog_diem_phu_nghiem_thu);
        $gb_summary->add_row('- Số đầu HD', $tong_so_hd_yeu_cau, $tong_so_hd_nghiem_thu);
		$gb_summary->add_row('- Số đầu SD', $tong_so_sd_yeu_cau, $tong_so_sd_nghiem_thu);
		$gb_summary->add_row('Tổng số phiếu NET', $tong_so_phieu_net_yeu_cau, $tong_so_phieu_net_nghiem_thu);
		foreach ($internetpackages as $name => $values) {
			$gb_summary->add_row('- ' . $name, $values[0], $values[1]);
		}
		
		$gb_summary->add_row('Tổng số phiếu trọn gói', $tong_so_phieu_tron_goi_yeu_cau, $tong_so_phieu_tron_goi_nghiem_thu);
		foreach ($bundles as $name => $values) {
			$gb_summary->add_row('- ' . $name, $values[0], $values[1]);
		}
		
		$this->display_module($gb_summary);
	}
    private function show_result_counselor($recs, $report, $counselor)
    {
        $gb = $this->init_module('Utils/GenericBrowser',null,'statictis');
        $gb->set_table_columns(array(
            array('name'=>__('Customer Group'), 'width'=>'90px'),
            array('name'=>__('Customer Name'), 'width'=>'120px'),
            array('name'=>__('Address'), 'width'=>'70px'),
            array('name'=>__('Street'), 'width'=>'100px'),
            array('name'=>__('Area'), 'width'=>'70px'),
            array('name'=>__('Ward'), 'width'=>'70px'),
            array('name'=>__('District'), 'width'=>'60px'),
            array('name'=>__('Phone'), 'width'=>'80px'),
            array('name'=>__('Cable Status'), 'width'=>'80px'),
            array('name'=>__('Main Analog'), 'width'=>'80px'),
            array('name'=>__('Sub Analog'), 'width'=>'80px'),
            array('name'=>__('Number Of HD'), 'width'=>'80px'),
            array('name'=>__('Number Of SD'), 'width'=>'80px'),
            array('name'=>__('Internet Package'), 'width'=>'90px'),
            array('name'=>__('Bundle'), 'width'=>'80px'),
            array('name'=>__('Counselor'), 'width'=>'120px'),
            array('name'=>__('Input Date'), 'width'=>'70px'),
            array('name'=>__('Input Time'), 'width'=>'70px'),
            array('name'=>__('Delivery Date'), 'width'=>'70px'),
            array('name'=>__('Requirement Code'), 'width'=>'70px'),
            array('name'=>__('Note'), 'width'=>'100px'),
            array('name'=>__('Deployment Date'), 'width'=>'90px'),
            array('name'=>__('Deployment Time'), 'width'=>'90px'),
            array('name'=>__('Acceptance Date'), 'width'=>'90px'),
            array('name'=>__('Deployment Employee'), 'width'=>'120px'),
            array('name'=>__('Deployment Cable Status'), 'width'=>'80px'),
            array('name'=>__('Deployment Main Analog'), 'width'=>'90px'),
            array('name'=>__('Deployment Sub Analog'), 'width'=>'90px'),
            array('name'=>__('Deployment HD'), 'width'=>'90px'),
            array('name'=>__('Deployment SD'), 'width'=>'90px'),
            array('name'=>__('Deployment Internet Package'), 'width'=>'100px'),
            array('name'=>__('Deployment Bundle'), 'width'=>'90px'),
            array('name'=>__('Deployment Business'), 'width'=>'90px'),
            array('name'=>__('Cable Contract Code'), 'width'=>'100px'),
            array('name'=>__('Internet Contract Code'), 'width'=>'100px'),
            array('name'=>__('Customer Code'), 'width'=>'100px'),
            array('name'=>__('Tap Code'), 'width'=>'100px'),
            array('name'=>__('Lend Wifi'), 'width'=>'50px'),
            array('name'=>__('Charge'), 'width'=>'100px'),
            array('name'=>__('Secondary Counselor'), 'width'=>'120px'),
            array('name'=>__('Secondary Main Analog'), 'width'=>'90px'),
            array('name'=>__('Secondary Sub Analog'), 'width'=>'90px'),
            array('name'=>__('Secondary HD'), 'width'=>'90px'),
            array('name'=>__('Secondary SD'), 'width'=>'90px'),
            array('name'=>__('Secondary Internet Package'), 'width'=>'100px')
        ));

        $tong_so_phieu_yeu_cau = 0;
        $tong_so_phieu_thc_yeu_cau = 0;
        $tong_so_main_analog_yeu_cau = 0;
        $tong_so_sub_analog_yeu_cau = 0;
        $tong_so_hd_yeu_cau = 0;
        $tong_so_sd_yeu_cau = 0;
        $tong_so_phieu_net_yeu_cau = 0;
        $tong_so_phieu_tron_goi_yeu_cau = 0;

        $tong_so_phieu_tu_van_them = 0;
        $tong_so_phieu_thc_tu_van_them = 0;
        $tong_so_main_analog_tu_van_them = 0;
        $tong_so_sub_analog_tu_van_them = 0;
        $tong_so_hd_tu_van_them = 0;
        $tong_so_sd_tu_van_them = 0;
        $tong_so_phieu_net_tu_van_them = 0;
        $tong_so_phieu_tron_goi_tu_van_them = 0;

        $tong_so_phieu_nghiem_thu = 0;
        $tong_so_phieu_thc_nghiem_thu = 0;
        $tong_so_main_analog_nghiem_thu = 0;
        $tong_so_sub_analog_nghiem_thu = 0;
        $tong_so_hd_nghiem_thu = 0;
        $tong_so_sd_nghiem_thu = 0;
        $tong_so_phieu_net_nghiem_thu = 0;
        $tong_so_phieu_tron_goi_nghiem_thu = 0;

        $internetpackages = array();
        $bundles = array();

        foreach ($recs as $key => $value){
            $tong_so_phieu_yeu_cau++;

            $main_analog = $value['main_analog'];
            $sub_analog = $value['sub_analog'];
            $number_of_hd = $value['number_of_hd'];
            $number_of_sd = $value['number_of_sd'];
            if($number_of_hd > 0 || $number_of_sd > 0 || $main_analog > 0 || $sub_analog > 0)
            {
                $tong_so_phieu_thc_yeu_cau++;
                if($main_analog > 0) {
                    $tong_so_main_analog_yeu_cau += $main_analog;
                }
                if($sub_analog > 0) {
                    $tong_so_sub_analog_yeu_cau += $sub_analog;
                }
                if($number_of_hd > 0)
                {
                    $tong_so_hd_yeu_cau += $number_of_hd;
                }
                if($number_of_sd > 0)
                {
                    $tong_so_sd_yeu_cau += $number_of_sd;
                }
            }

            $internet_package = $value['internet_package'];
            if($internet_package != '' && $internet_package != null)
            {
                $tong_so_phieu_net_yeu_cau++;

                if(array_key_exists($internet_package, $internetpackages))
                    $internetpackages[$internet_package][0] = $internetpackages[$internet_package][0] + 1;
                else
                {
                    $internetpackages[$internet_package] = array(1, 0);
                }
            }

            $bundle = $value['bundle'];
            if($bundle != '' && $bundle != null)
            {
                $tong_so_phieu_tron_goi_yeu_cau++;

                if(array_key_exists($bundle, $bundles))
                    $bundles[$bundle][0] = $bundles[$bundle][0] + 1;
                else
                {
                    $bundles[$bundle] = array(1, 0);
                }
            }

            $status = $value['status'];
            if($status == '1')
            {
                $tong_so_phieu_nghiem_thu++;

                $deployment_main_analog = $value['deployment_main_analog'];
                $deployment_sub_analog = $value['deployment_sub_analog'];
                $deployment_hd = $value['deployment_hd'];
                $deployment_sd = $value['deployment_sd'];
                if($deployment_hd > 0 || $deployment_sd > 0 || $deployment_main_analog > 0 || $deployment_sub_analog > 0)
                {
                    $tong_so_phieu_thc_nghiem_thu++;
                    if($deployment_main_analog > 0) {
                        $tong_so_main_analog_nghiem_thu += $deployment_main_analog;
                    }
                    if($deployment_sub_analog > 0) {
                        $tong_so_sub_analog_nghiem_thu += $deployment_sub_analog;
                    }
                    if($deployment_hd > 0)
                    {
                        $tong_so_hd_nghiem_thu += $deployment_hd;
                    }
                    if($deployment_sd > 0)
                    {
                        $tong_so_sd_nghiem_thu += $deployment_sd;
                    }
                }

                $internet_package = $value['deployment_internet_package'];
                if($internet_package != '' && $internet_package != null)
                {
                    $tong_so_phieu_net_nghiem_thu++;

                    if(array_key_exists($internet_package, $internetpackages))
                        $internetpackages[$internet_package][1] = $internetpackages[$internet_package][1] + 1;
                    else
                    {
                        $internetpackages[$internet_package] = array(0, 1);
                    }
                }

                $bundle = $value['deployment_bundle'];
                if($bundle != '' && $bundle != null)
                {
                    $tong_so_phieu_tron_goi_nghiem_thu++;

                    if(array_key_exists($bundle, $bundles))
                        $bundles[$bundle][1] = $bundles[$bundle][1] + 1;
                    else
                    {
                        $bundles[$bundle] = array(0, 1);
                    }
                }

                if(empty($value['secondary_counselor_id']) || $value['counselor_id'] == $value['secondary_counselor_id']){
                }
                else{
                    $secondary_main_analog = $value['secondary_main_analog'];
                    $secondary_sub_analog = $value['secondary_sub_analog'];
                    $secondary_hd = $value['secondary_hd'];
                    $secondary_sd = $value['secondary_sd'];
                    $secondary_internet_package = $value['secondary_internet_package'];
                    if($value['counselor_id'] == $counselor){
                        // Tru tu van them ra
                        $tong_so_main_analog_nghiem_thu = $tong_so_main_analog_nghiem_thu - $secondary_main_analog;
                        $tong_so_sub_analog_nghiem_thu = $tong_so_sub_analog_nghiem_thu - $secondary_sub_analog;
                        $tong_so_hd_nghiem_thu = $tong_so_hd_nghiem_thu - $secondary_hd;
                        $tong_so_sd_nghiem_thu = $tong_so_sd_nghiem_thu - $secondary_sd;
                        if(empty($secondary_internet_package)){
                        }
                        else{
                            if(array_key_exists($secondary_internet_package, $internetpackages))
                                $internetpackages[$secondary_internet_package][1] = $internetpackages[$secondary_internet_package][1] - 1;
                            else
                            {
                                $internetpackages[$secondary_internet_package] = array(0, 1);
                            }
                        }
                    }
                    elseif($value['secondary_counselor_id'] == $counselor){
                        $tong_so_main_analog_nghiem_thu = $tong_so_main_analog_nghiem_thu - $deployment_main_analog + $secondary_main_analog;
                        $tong_so_sub_analog_nghiem_thu = $tong_so_sub_analog_nghiem_thu - $deployment_sub_analog + $secondary_sub_analog;
                        $tong_so_hd_nghiem_thu = $tong_so_hd_nghiem_thu - $deployment_hd + $secondary_hd;
                        $tong_so_sd_nghiem_thu = $tong_so_sd_nghiem_thu - $deployment_sd + $secondary_sd;
                        if(empty($secondary_internet_package)){

                        }else{
                            if(array_key_exists($secondary_internet_package, $internetpackages)){
                                //$internetpackages[$secondary_internet_package][1] = $internetpackages[$secondary_internet_package][1] + 1;
                            }
                            else
                            {
                                $internetpackages[$secondary_internet_package] = array(0, 1);
                            }
                        }
                    }
                }
            }

            $gb->add_row(
                $value['customer_group'],
                $value['customer_name'],
                $value['address'],
                $value['street'],
                $value['area'],
                $value['ward'],
                $value['district'],
                $value['phone'],
                SCTV_RequirementCommon::get_cable_status_name($value['cable_status']),
                $value['main_analog'],
                $value['sub_analog'],
                $value['number_of_hd'],
                $value['number_of_sd'],
                $value['internet_package'],
                $value['bundle'],
                $value['counselor'],
                date('d-m-Y', strtotime($value['input_date'])),
                $value['input_time'],
                $value['delivery_date'],
                $value['requirement_code'],
                $value['note'],
                $value['deployment_date'] == null?null:date('d-m-Y', strtotime($value['deployment_date'])),
                $value['deployment_time'],
                $value['acceptance_date'] == null?null:date('d-m-Y', strtotime($value['acceptance_date'])),
                $value['deployment_employee'],
                SCTV_RequirementCommon::get_cable_status_name($value['deployment_cable_status']),
                $value['deployment_main_analog'],
                $value['deployment_sub_analog'],
                $value['deployment_hd'],
                $value['deployment_sd'],
                $value['deployment_internet_package'],
                $value['deployment_bundle'],
                $value['deployment_business'],
                $value['cable_contract_code'],
                $value['internet_contract_code'],
                $value['customer_code'],
                $value['tap_code'],
                SCTV_RequirementCommon::get_lend_wifi_name($value['lend_wifi']),
                $value['charge'],
                $value['secondary_counselor'],
                $value['secondary_main_analog'],
                $value['secondary_sub_analog'],
                $value['secondary_hd'],
                $value['secondary_sd'],
                $value['secondary_internet_package']
            );
        }

        $this->display_module($gb, array(true),'automatic_display');
        Base_ActionBarCommon::add('save', __('Export CSV'), 'href="modules/SCTV/Reports/'. $report . '_export.php?'.http_build_query(array('report'=>$report, 'cid'=>CID, 'path'=>$this->get_path())).'"');
        Base_ActionBarCommon::add('save', __('Export Excel'), 'href="modules/SCTV/Reports/'. $report . '_excel.php?'.http_build_query(array('report'=>$report, 'cid'=>CID, 'path'=>$this->get_path())).'"');

        $gb_summary = $this->init_module('Utils/GenericBrowser',null,'summary');
        $gb_summary->set_table_columns(array(
            array('name'=>__('Content'), 'width'=>'20%'),
            array('name'=>__('Requirement'), 'width'=>'10%'),
            array('name'=>__('Completed'), 'width'=>'10%')
        ));

        $gb_summary->add_row('Tổng số phiếu', $tong_so_phieu_yeu_cau, $tong_so_phieu_nghiem_thu);
        $gb_summary->add_row('Tổng số phiếu: THC', $tong_so_phieu_thc_yeu_cau, $tong_so_phieu_thc_nghiem_thu);
        $gb_summary->add_row('- Số Analog điểm chính', $tong_so_main_analog_yeu_cau, $tong_so_main_analog_nghiem_thu);
        $gb_summary->add_row('- Số Analog điểm phụ', $tong_so_sub_analog_yeu_cau, $tong_so_sub_analog_nghiem_thu);
        $gb_summary->add_row('- Số đầu HD', $tong_so_hd_yeu_cau, $tong_so_hd_nghiem_thu);
        $gb_summary->add_row('- Số đầu SD', $tong_so_sd_yeu_cau, $tong_so_sd_nghiem_thu);
        $gb_summary->add_row('Tổng số phiếu NET', $tong_so_phieu_net_yeu_cau, $tong_so_phieu_net_nghiem_thu);
        foreach ($internetpackages as $name => $values) {
            $gb_summary->add_row('- ' . $name, $values[0], $values[1]);
        }

        $gb_summary->add_row('Tổng số phiếu trọn gói', $tong_so_phieu_tron_goi_yeu_cau, $tong_so_phieu_tron_goi_nghiem_thu);
        foreach ($bundles as $name => $values) {
            $gb_summary->add_row('- ' . $name, $values[0], $values[1]);
        }

        $this->display_module($gb_summary);
    }

public function caption(){
		return 'Report';
	}
}
?>